package 优先算法.位运算.消失的两个数字;

import java.util.Arrays;

/**
 *https://leetcode.cn/problems/missing-two-lcci/
 */
public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        int[] arr = {1};
        int[] rel = so.missingTwo(arr);
        System.out.println(Arrays.toString(rel));
    }
}
class Solution {
    public int[] missingTwo(int[] nums) {
        int temp = 0,n = nums.length;
        for(int i = 0; i < n; i++) {
            temp ^= nums[i]^i;
        }
        temp^=n^(n+1)^(n+2);
        //找出第几个比特位为1
        int index = 0;
        while(true){
            if(((temp >> index) & 1)==1){
                break;
            }else{
                index++;
            }
        }

        //分两类 一类index为0 一类为1
        int[] rel = new int[2];
        for(int x : nums){
            if(((x >> index) & 1)==1){
                rel[0] ^= x;
            }else{
                rel[1] ^= x;
            }
        }
        for(int x = 0;x <= n+2 ; x++){
            if(((x >> index) & 1)==1){
                rel[0] ^= x;
            }else{
                rel[1] ^= x;
            }
        }
        return rel;
    }
}